package com.orderplus.opshop.common.util;

/**
 * 简单的键值对 id-name , name-value <br/>
 * 方便状态对应
 *
 * @author sunny
 */
public class NameItem {
	/**
	 * id
	 */
	public final int id;
	/**
	 * name
	 */
	public final String name;
	/**
	 * value
	 */
	public final Object value;

	/**
	 * nil
	 */
	public static final NameItem NAMEITEM_NIL = new NameItem(-1, "未知类型");

	private NameItem(int id, String name) {
		this.id = id;
		this.name = name;
		this.value = null;
	}

	private NameItem(String name, Object value) {
		this.id = -1;
		this.name = name;
		this.value = value;
	}

	private NameItem(String name, String value) {
		this.id = -1;
		this.name = name;
		this.value = value;
	}

	/**
	 * id-name
	 *
	 * @param id
	 * @param name
	 * @return
	 */
	public static NameItem valueOf(String name, int id) {
		return new NameItem(id, name);
	}

	/**
	 * name - value
	 *
	 * @param name
	 * @param value
	 * @return
	 */
	public static NameItem valueOf(String name, Object value) {
		return new NameItem(name, value);
	}

	/**
	 * name - value
	 *
	 * @param name
	 * @param value
	 * @return
	 */
	public static NameItem valueOf(String name, String value) {
		return new NameItem(name, value);
	}

	/**
	 * 根据id找到对应的item
	 *
	 * @param id
	 * @param item
	 * @return
	 */
	public static NameItem findItemById(int id, NameItem[] item) {
		if (null == item) {
			return NAMEITEM_NIL;
		}
		for (NameItem it : item) {
			if (id == it.id) {
				return it;
			}
		}
		return NAMEITEM_NIL;
	}

	/**
	 * 根据value找到对应的item
	 *
	 * @param id
	 * @param item
	 * @return
	 */
	public static NameItem findItenByValue(String value, NameItem[] item) {
		if (null == item || null == value || 0 == value.length()) {
			return NAMEITEM_NIL;
		}
		for (NameItem it : item) {
			if (value.equals(it.value)) {
				return it;
			}
		}
		return NAMEITEM_NIL;
	}

	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getValueStr() {
		return null == value ? "" : value.toString();
	}

	public Object getValueObj() {
		return value;
	}

	@Override
	public String toString() {
		return id + "-" + name;
	}
}
